package com.tfb.service.utility;

import java.net.URI;
import java.net.URLEncoder;

import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.apache.log4j.Logger;

public class ShortMessageSender {

    private static final Logger log = Logger.getLogger(ShortMessageSender.class);

    public ShortMessageSender() {
    }

    public static String send(String uri, String account, String pswd, String mobiles, String content,
            boolean needstatus, String product, String extno) {
        CloseableHttpClient httpClient = HttpClients.createDefault();

        HttpGet method = new HttpGet();
        try {
            String strUrl = uri + "HttpSendSM?" + "account=" + account + "&" + "pswd="
                    + URLEncoder.encode(pswd, "UTF-8") + "&mobile=" + mobiles + "&needstatus="
                    + String.valueOf(needstatus) + "&msg=" + URLEncoder.encode(content, "UTF-8");
            if (product != null && !product.isEmpty()) {
                strUrl += "&product=" + product;
            }
            if (extno != null && !extno.isEmpty()) {
                strUrl += "&extno=" + extno;
            }
            System.out.println(strUrl);
            URI url = new URI(strUrl);

            log.info("the send short message url is:" + strUrl);
            method.setURI(url);
            HttpResponse result = httpClient.execute(method);
            // URI base = new URI(uri, false);
            // method.setURI(new URI(base, "HttpSendSM", false));
            // method.setQueryString(new NameValuePair[]{new
            // NameValuePair("account", account), new NameValuePair("pswd",
            // pswd), new NameValuePair("mobile", mobiles), new
            // NameValuePair("needstatus", String.valueOf(needstatus)), new
            // NameValuePair("msg", content), new NameValuePair("product",
            // product), new NameValuePair("extno", extno)});
            // int result = client.executeMethod(method);
            String resData = EntityUtils.toString(result.getEntity());
            return resData;
        } catch (Exception e) {
            log.error(Utils.getFullException(e));
        }
        return null;
    }

    public static String batchSend(String uri, String account, String pswd, String mobiles, String content,
            boolean needstatus, String product, String extno) throws Exception {
        CloseableHttpClient httpClient = HttpClients.createDefault();

        HttpGet method = new HttpGet();
        try {
            String strUrl = uri + "HttpBatchSendSM?" + "account=" + account + "&" + "pswd="
                    + URLEncoder.encode(pswd, "UTF-8") + "&mobile=" + mobiles + "&needstatus="
                    + String.valueOf(needstatus) + "&msg=" + URLEncoder.encode(content, "UTF-8");
            if (product != null && !product.isEmpty()) {
                strUrl += "&product=" + product;
            }
            if (extno != null && !extno.isEmpty()) {
                strUrl += "&extno=" + extno;
            }
            System.out.println(strUrl);
            URI url = new URI(strUrl);

            log.info("the send short message url is:" + strUrl);
            method.setURI(url);
            HttpResponse result = httpClient.execute(method);
            // URI base = new URI(uri, false);
            // method.setURI(new URI(base, "HttpSendSM", false));
            // method.setQueryString(new NameValuePair[]{new
            // NameValuePair("account", account), new NameValuePair("pswd",
            // pswd), new NameValuePair("mobile", mobiles), new
            // NameValuePair("needstatus", String.valueOf(needstatus)), new
            // NameValuePair("msg", content), new NameValuePair("product",
            // product), new NameValuePair("extno", extno)});
            // int result = client.executeMethod(method);
            String resData = EntityUtils.toString(result.getEntity());
            return resData;
        } catch (Exception e) {
            log.error(Utils.getFullException(e));
        }
        return null;
    }
}
